package com.ykdl.app.Utils;

import java.io.File;
import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.ykdl.app.common.MyApplication;
/**
 * 像素转化的工具
 * @see
 * @since 1.0
 */

public class MobileUtils {
   private static  int screenWidthIntPX = 0;
   private static  int screenHeightIntpx = 0;
   
   public static int getScreenWidthIntPx(){
	   setScreenSize();
	   return screenWidthIntPX;
   }
   public static int getScreenHeightIntPx(){
	   setScreenSize();
	   return screenHeightIntpx;
   }
   private static void setScreenSize(){
	   WindowManager  manager = (WindowManager) MyApplication.getInstance().getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
       Display   display = manager.getDefaultDisplay();
       screenWidthIntPX = display.getWidth();
       screenHeightIntpx = display.getHeight();
   }
   
   public static DisplayMetrics  getDeviceDisplayMetrics(Context context){
	   WindowManager  manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
       Display   display = manager.getDefaultDisplay();
       DisplayMetrics  outMetrics = new DisplayMetrics();
       display.getMetrics(outMetrics);
       return outMetrics;
   }
   
   /**
    * 将dp转为px
    * @param dp
    * @return
    */
   public static int dpToPx(int dp){
	   return (int) (dp * getDeviceDisplayMetrics(MyApplication.getInstance()).density + 0.5f);
   }
   /**
    * 将px转为dp
    * @param dp
    * @return
    */
   public static int pxToDp(int px) {
		return (int) (px / getDeviceDisplayMetrics(MyApplication.getInstance()).density + 0.5f);
	}

   
   public static boolean isTopActivy(String cmdName){
       ActivityManager manager = (ActivityManager)MyApplication.getInstance().getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
       List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);
       String cmpNameTemp = null;
       if(null != runningTaskInfos){
               cmpNameTemp=((runningTaskInfos.get(0).topActivity).getClassName().toString());
               System.out.println("############ cmpNameTemp = " + cmpNameTemp);
       }
      if(null == cmpNameTemp)return false;
      return cmpNameTemp.equals(cmdName);

}


	public static void showKeyBorad(EditText editbox) {
		InputMethodManager imm = (InputMethodManager) MyApplication.getInstance().getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.showSoftInput(editbox, InputMethodManager.SHOW_FORCED);
	}

	public static void hideKeyBorad(EditText editbox) {
		InputMethodManager imm = (InputMethodManager) MyApplication.getInstance().getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(editbox.getWindowToken(), 0);
	}


	/**
	 * 获取当前应用程序的版本号
	 * 
	 * @return
	 */
	public static int getVersion() {
		try {
			final PackageManager manager = MyApplication.getInstance().getApplicationContext().getPackageManager();
			PackageInfo info = manager.getPackageInfo(MyApplication.getInstance().getApplicationContext().getPackageName(), 0);
			return info.versionCode ;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
   	public static String getStringVersion() {
		try {
			final PackageManager manager = MyApplication.getInstance().getApplicationContext().getPackageManager();
			PackageInfo info = manager.getPackageInfo(MyApplication.getInstance().getApplicationContext().getPackageName(), 0);
			return info.versionName ;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
   
	public static  void installApk(Context context, String saveFileNamePath) {
		File apkfile = new File(saveFileNamePath);
		if (!apkfile.exists()) {
			return;
		}
		Intent i = new Intent(Intent.ACTION_VIEW);
		i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		i.setDataAndType(Uri.parse("file://" + apkfile.toString()),"application/vnd.android.package-archive");
		context.startActivity(i);
	}
   
   
    public static void exitApp(Context context) {
        if (context != null) {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    }
   
   
   public static String getDeviceID(){
	   String	mDeviceID = "";
	   try {
		   TelephonyManager mTelephonyManager = (TelephonyManager) MyApplication.getInstance()
				   .getSystemService(Context.TELEPHONY_SERVICE);
		   	mDeviceID = mTelephonyManager.getDeviceId();
		
	} catch (Exception e) {
		e.printStackTrace();
	}
	return mDeviceID;
   }
   
   
   
}
